﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Buy2Site.Classes;
using System.IO;

namespace Buy2Site.UserControls
{
    public partial class ProductDetailsControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] == null)
            {                
                linkAddToCart.Enabled = false;
            }
            else
            {
                linkAddToCart.Enabled = true;
            }

        }

        public string Title 
        {
            get
            {
                return lblProductName.Text;
            }
            set
            {
                lblProductName.Text = value;
            }
        }
        public decimal Price 
        {
            get
            {
                return Convert.ToDecimal(lblPrice.Text);
            }
            set
            {
                lblPrice.Text = Convert.ToString(value);
            }
        }
        public DateTime Date 
        {
            get
            {
                return Convert.ToDateTime(lblDate.Text);
            }
            set
            {
                lblDate.Text = Convert.ToDateTime(value).ToShortTimeString();
            }
        }
        public string ShortDescription 
        {
            get
            {
                return lblShortDescrption.Text;
            }
            set
            {
                lblShortDescrption.Text = value;
            }
        }
        public string LongDescription
        {
            get
            {
                return lblLongDescrption.Text;
            }
            set
            {
                lblLongDescrption.Text = value;
            }
        }
        public string Image1 
        {
            get
            {
                return Picture1.ImageUrl;
            }
            set
            {
                Picture1.ImageUrl = value;
            }
        }
        public string Image2
        {
            get
            {
                return Picture2.ImageUrl;
            }
            set
            {
                Picture2.ImageUrl = value;
            }
        }
        public string Image3
        {
            get
            {
                return Picture3.ImageUrl;
            }
            set
            {
                Picture3.ImageUrl = value;
            }
        }

        protected void ActivateAddToCartEvent(object sender, EventArgs e)
        {
            // TODO
            // add to ListOfItems list - for session per user
            ((ProductClass)Session["ListOfItems"]).ListOfItems.Add(
                new ProductClass()
                {
                    Title = this.Title,
                    Price = Convert.ToDouble(this.Price),
                    Date = this.Date,
                    ShortDescription = this.ShortDescription,
                    LongDescription = this.LongDescription,
                    Picture1 = File.ReadAllBytes(this.Image1),
                    Picture2 = File.ReadAllBytes(this.Image2),
                    Picture3 = File.ReadAllBytes(this.Image3)
                });
            // add to ListOfAllQuantetiesItems list - for Application - to manage product quanteties
            ((ProductClass)Application["ListOfAllQuantetiesItems"]).ListOfAllQuantetiesItems.Add(
                new ProductClass()
                {
                    Title = this.Title,
                    Price = Convert.ToDouble(this.Price),
                    Date = this.Date,
                    ShortDescription = this.ShortDescription,
                    LongDescription = this.LongDescription,
                    Picture1 = File.ReadAllBytes(this.Image1),
                    Picture2 = File.ReadAllBytes(this.Image2),
                    Picture3 = File.ReadAllBytes(this.Image3)
                });
            //CartItemsManager.AddToCartList(new ProductClass()
            //{ 
            //    Title=this.Title,
            //    Price= Convert.ToDouble(this.Price),
            //    Date=this.Date,
            //    ShortDescription=this.ShortDescription,
            //    LongDescription=this.LongDescription,
            //    Picture1=File.ReadAllBytes(this.Image1),
            //    Picture2=File.ReadAllBytes(this.Image2),
            //    Picture3=File.ReadAllBytes(this.Image3)
            //});            
        }
    }
}